﻿using System;
using System.Globalization;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CourseUtils;

// http://www.platesh.ru/annuitetnie-plateshi/

namespace creditCalculator
{

    public class CurrentPayment
    {
        public int creditSum;  // The sum which customer got from a creditor bank.
        public int rate;                          // This is the annual percentage rate.
        public int timePeriodOfCreditInMonth;
        public DateTime startDate;                // Date when a customer got a credit.
        public DateTime paymentDate;              // Date when a customer should make a current payment.
        public int numberOfPayment;               // The payment sequence number.
        public double bodyPart;                    // The body part of a current credit payment
        public double percentagesPart;               // The percentages part of a current credit payment
        public double debtAfterCurrentPayment;       // The remaining part of the current debt after payment by customer.
        public double AnnuityMonthlyPayment;         // A payment monthly sum which consist of 2 parts as creditPart and percentagesPart(?).

        public void inputData()
        {
            creditSum = CourseUtils.IOHelpers.ReadIntFromConsole("Input credit sum in UAN");
            rate = CourseUtils.IOHelpers.ReadIntFromConsole("Input rate in %");
            timePeriodOfCreditInMonth = CourseUtils.IOHelpers.ReadIntFromConsole("Input time period in months");
            return;
        }

        public DateTime ReadDateFromConsole(String displayText)
        {
            Console.WriteLine(displayText);

            //   int year = IOHelpers.ReadIntFromConsole("\nEnter the year:", 1900, 2015);
            int year = 2015; // --------
            //   int month = IOHelpers.ReadIntFromConsole("Enter the month number:", 1, 12);
            int month = 1; // --------------
            //   int day = IOHelpers.ReadIntFromConsole("Enter the day:", 1, 31);
            int day = 5; // -------------

            return new DateTime(year, month, day);
        }

        public void calculateNumberOfPayment()
        {
            numberOfPayment += 1;
            return;
        }

        public void calculatePaymentDate(int numberOfPayment)
        {
            paymentDate = startDate;
            paymentDate = paymentDate.AddMonths(numberOfPayment);
            if (paymentDate.DayOfWeek == DayOfWeek.Saturday)
            {
                paymentDate = paymentDate.AddDays(-1);
            }
            if (paymentDate.DayOfWeek == DayOfWeek.Sunday)
            {
                paymentDate = paymentDate.AddDays(-2);
            }
            return;
        }

        public void calculateAnnuityMonthlyPayment()
        { // calculation according to the main big formular.
            double monthlyRate = (double)(rate * 0.01 / 12.0);
            double intermediatePart = (double)Math.Pow(1.0 + monthlyRate, timePeriodOfCreditInMonth);
            AnnuityMonthlyPayment = creditSum * (monthlyRate + monthlyRate / (intermediatePart - 1));
            return;
        }

        public void calculateDebtAfterCurrentPayment()
        {
            debtAfterCurrentPayment -= AnnuityMonthlyPayment - percentagesPart;
            return;
        }

        public void calculatePercentagesPart()
        {
            percentagesPart = debtAfterCurrentPayment * (double)rate * 0.01 / 12.0;
            return;
        }

        public void calculateBodyPart()
        {
            bodyPart = AnnuityMonthlyPayment - percentagesPart;
            return;
        }
        //--------------------
    }



    class Program
    {
        static public void printHeader()
        {
            Console.BackgroundColor = ConsoleColor.Cyan;
            Console.ForegroundColor = ConsoleColor.Black;
            Console.Title = "Credit calculator";
            int height = 35;
            int WindowWidth = 200;
            Console.WindowHeight = height;
            int WindowTop = Console.WindowTop;
            WindowWidth = Console.WindowWidth;
            Console.Clear();
            Console.WriteLine("|Number |  Payment day\t  |  BodyPart\t| PercentagesPart|Remainder of loan|");

        }

        static void Main(string[] args)
        {
            // Set the current culture to english
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
            //-------------------------------
            CurrentPayment StringOfTable = new CurrentPayment();  // <---------------------------- NEW ------------
            StringOfTable.rate = 10;
            StringOfTable.creditSum = 100000;
            StringOfTable.debtAfterCurrentPayment = StringOfTable.creditSum;
            StringOfTable.timePeriodOfCreditInMonth = 6;
            //===============================
            //           CurrentPayment.inputData();

            StringOfTable.startDate = StringOfTable.ReadDateFromConsole("Please enter date of receiving of the credit");
            StringOfTable.calculateAnnuityMonthlyPayment();
            for (int i = 1; i <= StringOfTable.timePeriodOfCreditInMonth; i++)
            {
                if (i == 1)
                {
                    printHeader();
                }
                StringOfTable.calculateNumberOfPayment();
                StringOfTable.calculatePercentagesPart();
                StringOfTable.calculateBodyPart();
                StringOfTable.calculateDebtAfterCurrentPayment();
                StringOfTable.calculatePaymentDate(StringOfTable.numberOfPayment);
                Console.WriteLine("| {0}\t|{1:ddd MMM dd, yyyy} | {2:f2}$\t| {3:f2}$\t | {4,14:f2}$ |", StringOfTable.numberOfPayment,
                    StringOfTable.paymentDate, StringOfTable.bodyPart, StringOfTable.percentagesPart,
                    StringOfTable.debtAfterCurrentPayment);

            }
            Console.ReadKey();
        }
    }
}
